package com.lonwin.inspectionmanagement.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;
import com.lonwin.inspectionmanagement.R2;
import com.lonwin.inspectionmanagement.app.utils.TimeUtils;
import com.lonwin.inspectionmanagement.di.component.DaggerSortOutRecordSearchComponent;
import com.lonwin.inspectionmanagement.mvp.contract.SortOutRecordSearchContract;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.ConfigBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.SortOutBean;
import com.lonwin.inspectionmanagement.mvp.presenter.SortOutRecordSearchPresenter;

import com.lonwin.inspectionmanagement.R;
import com.lonwin.inspectionmanagement.mvp.ui.adapter.SortOutRecordAdapter;
import com.lonwin.inspectionmanagement.mvp.ui.fragment.SortOutRecordFragment;
import com.lonwin.inspectionmanagement.widget.dialog.LoadingDailog;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 12/18/2018 21:38
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class SortOutRecordSearchActivity extends BaseActivity<SortOutRecordSearchPresenter> implements SortOutRecordSearchContract.View,OnDateSetListener {

    @BindView(R2.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R2.id.sortout_record_RecyclerView)
    RecyclerView sortoutRecordRecyclerView;

    @BindView(R2.id.sortout_record_startTime)
    TextView sortout_record_startTime;
    @BindView(R2.id.sortout_record_endTime)
    TextView sortout_record_endTime;
    @BindView(R2.id.commonaddress_refresh_layout)
    SmartRefreshLayout commonaddressRefreshLayout;
    Unbinder unbinder;
    private SortOutRecordAdapter sortOutRecordAdapter;
    private boolean isRefersh = false;
    int allpages=0;
    int pageIndex=1;
    private TimePickerDialog mDialogAll;
    private LoadingDailog loadingDailog;
    ConfigBean.RiverDatasBean.ChildrenBean childrenBean;
    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerSortOutRecordSearchComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_sort_out_record_search; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
       if (!getIntent().hasExtra("ChildrenBean"))
       {
           killMyself();
           return;
       }
       childrenBean=(ConfigBean.RiverDatasBean.ChildrenBean)getIntent().getSerializableExtra("ChildrenBean");

        loadingDailog=new LoadingDailog.Builder(this).setMessage("查询中...").create();
        sortout_record_startTime.setText(TimeUtils.getFirstDayAndFirstMonth());
        sortout_record_endTime.setText(TimeUtils.getNowDayAndMonth());
        initTime();
        sortoutRecordRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        sortOutRecordAdapter=new SortOutRecordAdapter(new ArrayList<>());
        sortOutRecordAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
        sortoutRecordRecyclerView.setAdapter(sortOutRecordAdapter);


        commonaddressRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                isRefersh = true;
                allpages=0;
                pageIndex=1;
                commonaddressRefreshLayout.finishRefresh(1500);
                mPresenter.getDigestedXcjlIndex(sortout_record_startTime.getText().toString(),sortout_record_endTime.getText().toString(),pageIndex,childrenBean.getId());
//                mPresenter.getUndigestedXcjl(sortout_record_startTime.getText().toString(),sortout_record_endTime.getText().toString(),pageIndex);
            }
        });
        commonaddressRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                isRefersh = false;
                if (allpages==pageIndex||allpages<pageIndex)
                {
                    ArmsUtils.makeText(SortOutRecordSearchActivity.this,"已经全部加载");
                    commonaddressRefreshLayout.finishLoadMore();
                    return;
                }
                pageIndex++;
                commonaddressRefreshLayout.finishLoadMore(1000);
                mPresenter.getDigestedXcjlIndex(sortout_record_startTime.getText().toString(),sortout_record_endTime.getText().toString(),pageIndex,childrenBean.getId());

            }
        });
        sortout_record_startTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialogAll.show(getSupportFragmentManager(),"开始时间");

            }
        });
        sortout_record_endTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mDialogAll.show(getSupportFragmentManager(),"结束时间");
            }
        });
    }
    public void initTime(){
        mDialogAll = new TimePickerDialog.Builder()
                .setCallBack(SortOutRecordSearchActivity.this)
                .setCancelStringId("取消")
                .setSureStringId("确定")
                .setTitleStringId("选择查询时间")
                .setYearText("年")
                .setMonthText("月")
                .setDayText("日")
//                         .setHourText("Hour")
//                         .setMinuteText("Minute")
                .setCyclic(false)
//            .setMinMillseconds(System.currentTimeMillis()-(100*365*24*60*60*1000))
//                         .setMaxMillseconds(System.currentTimeMillis()+10*1000 )
                .setCurrentMillseconds(System.currentTimeMillis())
                .setThemeColor(getResources().getColor(R.color.color_FF098DDB))
                .setType(Type.YEAR_MONTH_DAY)
//                         .setWheelItemTextNormalColor(getResources().getColor(R.color.timetimepicker_default_text_color))
//                         .setWheelItemTextSelectorColor(getResources().getColor(R.color.timepicker_toolbar_bg))
                .setWheelItemTextSize(14)
                .build();
    }
    @Override
    public void onResume() {
        super.onResume();
        commonaddressRefreshLayout.autoRefresh();
        if (childrenBean!=null)
        {
            toolbarTitle.setText(childrenBean.getName()+"巡查记录");
        }
    }

    @Override
    public void showLoading() {
        loadingDailog.show();
    }

    @Override
    public void hideLoading() {
        loadingDailog.dismiss();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public Activity getMyActivity() {
        return this;
    }

    @Override
    public void getDigestedXcjlIndexSuccess(List<SortOutBean.DataBean.RecordsBean> recordsBeans) {
        if (recordsBeans==null||recordsBeans.isEmpty())
        {
            sortOutRecordAdapter.setEmptyView(ArmsUtils.inflate(this,R.layout.empty_view));
            return;
        }
        sortOutRecordAdapter.setNewData(recordsBeans);
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data!=null&&requestCode==1001)
        {
            ConfigBean.RiverDatasBean.ChildrenBean childrenBean=(ConfigBean.RiverDatasBean.ChildrenBean)data.getSerializableExtra("ChildrenBean");
        }
    }

    @Override
    public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
        timePickerView.dismiss();
        if (timePickerView.getTag().equals("开始时间"))
        {
            sortout_record_startTime.setText(TimeUtils.longToStrTime(millseconds));

        } else {
            sortout_record_endTime.setText(TimeUtils.longToStrTime(millseconds));
        }
        if (sortout_record_startTime.getText().toString().compareTo(sortout_record_endTime.getText().toString())<0)
        {
            commonaddressRefreshLayout.autoRefresh();
            return;
        }
        showMessage("开始时间不能大于结束时间");
    }
}


